package cn.francis.controller;

import cn.francis.service.CompanyService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qfedu.agro.common.ResultInfo;
import com.qfedu.agro.pojo.Company;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Name: CompanyController
 * Package: cn.francis.controller
 * Date: 2024/10/26 - 17:23
 * Description:
 *
 * @author Junhui Zhang
 * @version v1.0
 */

@RestController
@RequestMapping("/company")
public class CompanyController {

    @Autowired
    private CompanyService companyService;

    @GetMapping("/page")
    public ResultInfo findPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer pageSize, String name) {
        Page<Company> pageContent = companyService.findPage(page, pageSize, name);
        return ResultInfo.success(pageContent);
    }

    @DeleteMapping
    public ResultInfo delete(Long id) {
        try {
            companyService.delete(id);
            return ResultInfo.success(null);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    @PostMapping
    public ResultInfo insert(@RequestBody Company company) {
        try {
            companyService.insert(company);
            return ResultInfo.success(null);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }
}
